#include "options.h"

#include <string.h>
#include <unistd.h>
#include <getopt.h>

static struct option long_options[] = {
	{"config", required_argument, 0, 'c'},
	{"username", required_argument, 0, 'u'},
	{"password", required_argument, 0, 'p'},
	{"hostname", required_argument, 0, 'h'},
	{"ip", required_argument, 0, 'i'},
	{"wildcard", required_argument, 0, 'w'},
	{"mx", required_argument, 0, 'm'},
	{"backmx", required_argument, 0, 'b'},
	{"offline", required_argument, 0, 'o'},
	{"verbose", no_argument, 0, 'v'},
	{"version", no_argument, 0, 'V'},
	{"help", no_argument, 0, 'H'},
	{0, 0, 0, 0}
};

extern void options_parse(routerdns_t *rdns, int argc, char *argv[])
{
	while (1) {
		int option_index = 0;

		/* parse the options */
		int c = getopt_long(argc, argv, "c:u:p:h:i:w:m:b:o:v", long_options,
			&option_index);
		
		if (c == -1)
			break;

		switch (c) {
			case 'c':
				/*options_config(rdns, optarg);*/
				break;
			case 'u':
				rdns->username = optarg;
				break;
			case 'p':
				rdns->password = optarg;
				break;
			case 'h':
				rdns->hostname = optarg;
				break;
			case 'i':
				rdns->ip = optarg;
				break;
			case 'w':
				rdns->wildcard = (strcmp(optarg, "off") == 0) ? OFF : ON;
				break;
			case 'm':
				rdns->mx = optarg;
				break;
			case 'b':
				rdns->backmx = (strcmp(optarg, "off") == 0) ? OFF : ON;
				break;
			case 'o':
				rdns->offline = (strcmp(optarg, "off") == 0) ? OFF : ON;
				break;
			case 'v':
				rdns->verbose = ON;
				break;
			case 'H':
				usage();
				break;
			case 'V':
				version();
				break;
		}
	}
	
	/* do we got the required information */
	if (rdns->hostname == NULL || rdns->username == NULL ||
		rdns->password == NULL)
	{
		usage();
	}
	
	/* debug information */
	if (rdns->verbose == ON) {
		char *readable[3] = {
			"off",
			"on",
			"no change"
		};
		
		printf("routerdns configuration:\nusername = %s\npassword = %s\n"
			"hostname = %s\nip = %s\nwildcard = %s\nmx = %s\nbackmx = %s\n"
			"offline = %s\nverbose = %s\n", rdns->username, rdns->password,
			rdns->hostname, rdns->ip, readable[rdns->wildcard], rdns->mx,
			readable[rdns->backmx], readable[rdns->offline],
			readable[rdns->verbose]);
	}
}